package com.baox.agridata.synchronous.controller;


import com.baox.agridata.synchronous.entity.TUserDeclare;
import com.baox.agridata.synchronous.schedule.LocalPushDataToScAgriculture;
import com.baox.agridata.synchronous.schedule.SiChuanToChengDuBaseInfoSchedule;
import com.baox.agridata.synchronous.schedule.SiChuanToChengDuLandingFeedbackSchedule;
import com.baox.agridata.synchronous.schedule.SiChuanToChengDuSchedule;
import com.baox.agridata.synchronous.service.TUserDeclareService;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/test")
public class TestController {


    @GetMapping("/test0")
    public String test0() {
        return "测试成功";
    }

    @Autowired
    private SiChuanToChengDuSchedule siChuanToChengDuSchedule;
    @Autowired
    private TUserDeclareService tUserDeclareService;

    @GetMapping("/test1")
    public void test1() {
        siChuanToChengDuSchedule.synchronizationTCCABToChengdu();
    }


    @GetMapping("/test2")
    public void test2() {
        siChuanToChengDuSchedule.synchronizationTCCPBToChengdu();
    }


    @GetMapping("/test3")
    public void test3() {
        siChuanToChengDuSchedule.synchronizationTCCOutAAToChengdu();
    }


    @GetMapping("/test4")
    public void test4() {
        siChuanToChengDuSchedule.synchronizationTCCOutPAToChengdu();
    }


    @Autowired
    private SiChuanToChengDuLandingFeedbackSchedule siChuanToChengDuLandingFeedbackSchedule;

    @GetMapping("/test7")
    public void test7() {
        siChuanToChengDuLandingFeedbackSchedule.synchronizationTCCABLandingToChengdu();
    }

//    @GetMapping("/test8")
//    public void test8() {
//        siChuanToChengDuLandingFeedbackSchedule.synchronizationTCCPBLandingToChengdu();
//    }


    @GetMapping("/test11")
    public void test11() {
        siChuanToChengDuLandingFeedbackSchedule.synchronizationTCCAALandingToChengdu();
    }

//    @GetMapping("/test12")
//    public void test12() {
//        siChuanToChengDuLandingFeedbackSchedule.synchronizationTCCPALandingToChengdu();
//    }


    @Autowired
    private SiChuanToChengDuBaseInfoSchedule siChuanToChengDuBaseInfoSchedule;


    @GetMapping("/test5")
    public void test5() {
        siChuanToChengDuBaseInfoSchedule.synchronizationTXDRCarToChengDu();
    }

    @GetMapping("/test6")
    public void test6() {
        siChuanToChengDuBaseInfoSchedule.synchronizationTXDRSlaughterHouseToChengDu();
    }

    @GetMapping("/test9")
    public void test9() {
        siChuanToChengDuBaseInfoSchedule.synchronizationXDRTraffickingToChengDu();
    }

    @GetMapping("/test10")
    public void test10() {
        siChuanToChengDuBaseInfoSchedule.synchronizationTBaseRegionToChengdu();
    }

    @GetMapping("/test13")
    public void test13() {
        siChuanToChengDuBaseInfoSchedule.synchronizationZsEarTagRecordToChengdu();
    }



    @GetMapping("/test14")
    public void test14() {
        siChuanToChengDuSchedule.synchronizationCheckPointResultToChengdu();
    }


    @Autowired
    private LocalPushDataToScAgriculture localPushDataToScAgriculture;

    @GetMapping("/test15")
    public void test15() {
        localPushDataToScAgriculture.pushXDRBaseData();
    }

    @GetMapping("/test16")
    public void test16() {
        localPushDataToScAgriculture.pushHarmlessData();
    }
    @GetMapping("/test17")
    public void test17() {
        localPushDataToScAgriculture.pushLiveStockMarketData();
    }

    @GetMapping("/test18")
    public void test18() {
        localPushDataToScAgriculture.pushCertificateData();
    }

    @GetMapping("/test19")
    public void test19() {
        localPushDataToScAgriculture.pushOutProvinceCertificateData();
    }

    @GetMapping("/test20")
    public void test20() {
        localPushDataToScAgriculture.pushInProvinceCertificateData();
    }

    @GetMapping("/deleteImage")
    public void deleteImage() {
        try {
            List<TUserDeclare> tUserDeclares = tUserDeclareService.selectUserDeclare();
            for (TUserDeclare tUserDeclare : tUserDeclares) {
                StringBuilder deleteStatus = new StringBuilder();
                String image = tUserDeclare.getImage();
                String[] imageArr = image.split(",");
                for (String a : imageArr) {
                    System.out.println("image: " + a);
                    String physicalPath = tUserDeclareService.selectPhysicalPath(a); // 图片的绝对路径
                    System.out.println("physicalPath: " + physicalPath);

                    if (physicalPath == null || "".equals(physicalPath) || physicalPath.length() == 0) {
                        deleteStatus.append("1,");  // 有图片ID，但映射表没有数据
                    } else {
                        File localFile = new File(physicalPath);
                        String fileName = localFile.getName();
                        // 迁移文件到目标服务器的路径
                        String targetBaseDir = "/data/sichuanex//storage/";
                        String targetServerPath = physicalPath.substring("/data/sichuanex//storage/".length(), physicalPath.lastIndexOf(fileName));  // 修改为目标服务器路径
                        // 目标路径为远程存储根目录 + 文件路径 + 文件名
                        String targetPath = targetBaseDir + "/" + targetServerPath;
                        System.out.println("目标路径："+targetPath);
                        // 假设我们有一个方法来将文件迁移到目标服务器
//                        boolean isMigrated = migrateFile(physicalPath, targetPath);

//                        if (isMigrated) {
//                            // 如果迁移成功，删除原文件
//                            File file = new File(physicalPath);
//                            if (file.delete()) {
//                                deleteStatus.append("2,"); // 根据mid找到图片绝对路径，并且删除成功
//                                System.out.println("文件迁移成功并删除成功");
//                            } else {
//                                deleteStatus.append("3,"); // 删除失败
//                                System.out.println("文件迁移成功，但删除失败");
//                            }
//                        } else {
//                            deleteStatus.append("4,"); // 迁移失败
//                            System.out.println("文件迁移失败");
//                        }
                    }
                }
                tUserDeclareService.updateDeleteStatus(tUserDeclare.getMid(), deleteStatus.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean migrateFile(String sourcePath, String targetPath) {
        String remoteHost = "example.com"; // 目标服务器地址
        String remoteUser = "username";    // 目标服务器用户名
        String remotePassword = "password";// 目标服务器密码
        int port = 22; // 通常是 22，除非你使用自定义端口

        try {
            // 创建 JSch 实例
            JSch jsch = new JSch();

            // 获取会话信息
            Session session = jsch.getSession(remoteUser, remoteHost, port);
            session.setPassword(remotePassword);

            // 配置会话，避免因没有目标主机密钥而产生异常
            session.setConfig("StrictHostKeyChecking", "no");

            // 建立连接
            session.connect();

            // 获取 SFTP 通道
            ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
            sftpChannel.connect();

            // 上传文件
            sftpChannel.put(sourcePath, targetPath);  // sourcePath 是本地路径，targetPath 是远程目标路径

            // 断开连接
            sftpChannel.disconnect();
            session.disconnect();

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
